using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.IService.Sys;

namespace net_work.Controllers.Sys;

[Route("api/[controller]")]
[ApiController]
public class TeacherController(ILogger<TeacherController> logger, ITeacherService teacherService) : ControllerBase
{
    /// <summary>
    /// 获取教师列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListTeachers([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取教师列表");
        var result = await teacherService.GetTeachersPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取教师信息
    /// </summary>
    /// <param name="teacherId">教师ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetTeacherById([FromQuery] long teacherId)
    {
        logger.LogInformation("获取教师信息");
        return await teacherService.SelectByIdAsync(teacherId) is { } teacher
            ? AjaxResult.Success(teacher)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增教师
    /// </summary>
    /// <param name="teacherAddRequest">新增教师实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddTeacher([FromBody] TeacherAddRequest teacherAddRequest)
    {
        logger.LogInformation("新增教师");
        var teacherId = await teacherService.InsertTeacher(teacherAddRequest);
        return teacherId > 0
            ? AjaxResult.Success(new { teacherId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改教师
    /// </summary>
    /// <param name="teacherUpdateRequest">修改教师实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize]
    public async Task<IActionResult> UpdateTeacher([FromBody] TeacherUpdateRequest teacherUpdateRequest)
    {
        logger.LogInformation("修改教师");
        return await teacherService.UpdateTeacher(teacherUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除教师
    /// </summary>
    /// <param name="ids">教师ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteTeacher([FromBody] List<long> ids)
    {
        logger.LogInformation("删除教师");
        return await teacherService.DeleteTeacherByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}